package com.robotserver.orders.jobs;

import com.robotserver.orders.param.VipOrderSyncParam;
import com.robotserver.orders.service.VipOrderService;
import com.robotserver.orders.service.VipRefundOrderService;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/**
 * @Auther: jianghan
 * @Date: 2020/12/2 11:28
 * @Description:
 */
@Component
public class VipRefundOrderSyncJob {
    private static Logger logger = LoggerFactory.getLogger(VipRefundOrderSyncJob.class);

    @Autowired
    private VipRefundOrderService vipRefundOrderService;
    @Autowired
    private RedissonClient redissonClient;
    @Value("${cluster.group}")
    private String clusterGroup;

    /**
     * 每分钟执行一次拉取维权订单（时间范围是3分钟）
     */
//    @Scheduled(cron="0 0/1 * * * ?")
    public void execute(){
        logger.debug("VipRefundOrderSyncJob execute.");

        RLock lock = redissonClient.getLock(clusterGroup+"_service_vip_refund_order_sync_lock");
        boolean getlock = false;
        try {
            if (getlock=lock.tryLock(0,1, TimeUnit.MINUTES)) {
                logger.debug("execute right.");
                Long tmptime = System.currentTimeMillis();
                Long end_time = tmptime ;                       //截至时间为当前时间
                Long begin_time = tmptime - 180000;             //起始时间为前3分钟
                VipOrderSyncParam vipOrderSyncParam = new VipOrderSyncParam();
                vipOrderSyncParam.setBegin_time(begin_time);
                vipOrderSyncParam.setEnd_time(end_time);
                vipRefundOrderService.syncOrder(vipOrderSyncParam);
            }else {
                logger.debug("not execute right.");
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (getlock){
                lock.unlock();
                logger.debug("release lock.");
            }
        }
    }
}
